package com.sjgtw.cloud.app.controller;

import com.sjgtw.cloud.common.core.constant.CacheConstants;
import com.sjgtw.cloud.common.core.exception.BaseException;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.service.SystemServerService;
import com.sjgtw.cloud.system.api.model.LoginUser;
import com.sjgtw.cloud.web.cuttingSlopeProtection.domain.CuttingSlopeProtection;
import com.sjgtw.cloud.web.cuttingSlopeProtection.service.ICuttingSlopeProtectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 路堑护坡Controller
 * 
 * @author ruoyi
 * @date 2024-03-07
 */
@RestController
@RequestMapping("/app/cuttingSlopeProtection")
@Api(tags = "6.3.2、APP应急设施-路堑护坡")
public class AppCuttingSlopeProtectionController extends BaseController
{

    @Autowired
    private ICuttingSlopeProtectionService cuttingSlopeProtectionService;

    @Autowired
    private SystemServerService systemServerService;

    @Autowired
    private RedisService redisService;

    /**
     * 查询路堑护坡列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询路堑护坡列表")
    public TableDataInfo list(CuttingSlopeProtection cuttingSlopeProtection)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);
        if (user == null) {
            throw new BaseException("登录信息失效，请重新登录");
        }
        cuttingSlopeProtection.setCreateUserId(user.getUserid());
 /*       if (user != null) {
            AjaxResult ajaxResult = systemServerService.getUserDeptLevel(user.getUserid());
            if (ajaxResult != null) {
                Object object = ajaxResult.get("data");
                if (object != null) {
                    JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(object));
                    String dept = jsonObject.getString("dept");
                    Long deptId= jsonObject.getLong("deptId");
                    Long parentId= jsonObject.getLong("parentId");
                    if("section".equals(dept)){
                        cuttingSlopeProtection.setSection(deptId);
                    }else if("workArea".equals(dept)){
                        cuttingSlopeProtection.setSection(parentId);
                        cuttingSlopeProtection.setWorkArea(deptId);
                    }
                }

            }
        }*/

        startPage();
        List<CuttingSlopeProtection> list = cuttingSlopeProtectionService.selectCuttingSlopeProtectionList(cuttingSlopeProtection);
        return getDataTable(list);
    }

    /**
     * 导出路堑护坡列表
     */
  /*  @Log(title = "路堑护坡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public void export(HttpServletResponse response, CuttingSlopeProtection cuttingSlopeProtection)
    {
        List<CuttingSlopeProtection> list = cuttingSlopeProtectionService.selectCuttingSlopeProtectionList(cuttingSlopeProtection);
        ExcelUtil<CuttingSlopeProtection> util = new ExcelUtil<CuttingSlopeProtection>(CuttingSlopeProtection.class);
        try {
            util.exportExcel(response, list, "路堑护坡数据");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/

    /**
     * 新增保存路堑护坡
     */
    @ApiOperation(value = "新增保存路堑护坡")
//    @Log(title = "路堑护坡", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody CuttingSlopeProtection cuttingSlopeProtection, HttpServletResponse response)
    {

        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);
        if (user == null) {
            throw new BaseException("登录信息失效，请重新登录");
        }

        cuttingSlopeProtection.setCreateUserId(user.getUserid());
     /*   if (user != null) {
            AjaxResult ajaxResult = systemServerService.getUserDeptLevel(user.getUserid());
            if (ajaxResult != null) {
                Object object = ajaxResult.get("data");
                if (object != null) {
                    JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(object));
                    String dept = jsonObject.getString("dept");
                    Long deptId= jsonObject.getLong("deptId");
                    Long parentId= jsonObject.getLong("parentId");
                    if("section".equals(dept)){
                        cuttingSlopeProtection.setSection(deptId);
                    }else if("workArea".equals(dept)){
                        cuttingSlopeProtection.setSection(parentId);
                        cuttingSlopeProtection.setWorkArea(deptId);
                    }
                }

            }
        }*/
        return toAjax(cuttingSlopeProtectionService.insertCuttingSlopeProtection(cuttingSlopeProtection));
    }

    /**
     * 修改保存路堑护坡
     */
//    @Log(title = "路堑护坡", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "修改保存路堑护坡")
    public AjaxResult editSave(@RequestBody CuttingSlopeProtection cuttingSlopeProtection)
    {
        return toAjax(cuttingSlopeProtectionService.updateCuttingSlopeProtection(cuttingSlopeProtection));
    }

    /**
     * 删除路堑护坡
     */
//    @Log(title = "路堑护坡", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ApiOperation(value = "删除路堑护坡")
    public AjaxResult remove(String[] ids)
    {
        return toAjax(cuttingSlopeProtectionService.deleteCuttingSlopeProtectionByIds(ids));
    }

    /**
     * 获取路堑护坡详细信息
     */
    @GetMapping("/getInfo")
    @ApiOperation(value = "获取路堑护坡详细信息")
    public AjaxResult getInfo(Long id)
    {
        return AjaxResult.success(cuttingSlopeProtectionService.selectCuttingSlopeProtectionById(id));
    }


}
